Ext.define('Seq.app.WidgetController', {
    extend: 'Seq.app.BaseController',

    onRefreshData(view, record, params, opts) {
        let me = this;
        params = me.getExtraParams(params);
        if (me.validateParams(params)) {
            me.refreshWidgets(params);
        }
    },

    validateParams(params) {
        return true;
    },

    refreshWidgets(params) {
        let me = this;
        me.refreshValues(params);
        me.refreshCharts(params);
        me.refreshDataViewer(params);
    },

    refreshValues(params) {
        let me = this, vm = me.getViewModel(), widgets = Ext.clone(vm.widgets);
        if (!Ext.isArray(widgets)) {
            widgets = [widgets];
        }
        let typeWidgets = {};
        for (let item of widgets) {
            // 页面参数
            let defaults = item.defaults || {}, params_c = Ext.clone(params);
            defaults.tags = Ext.applyIf(defaults.tags || {}, params_c.tags);
            delete params_c.tags;
            defaults = Ext.applyIf(defaults, params_c);
            // defaults处理
            for (let name in item) {
                let defaults_c = Ext.clone(defaults);
                if (defaults_c && name != "defaults") {
                    let widget = item[name];
                    Seq.Array.applyIf(widget.tags, defaults_c.tags);
                    delete defaults_c.tags;
                    Ext.applyIf(widget, defaults_c);
                    widget.name = name;

                    let rest = widget.rest
                    if (!typeWidgets[rest]) {
                        typeWidgets[rest] = [];
                    }
                    typeWidgets[rest].push(widget);
                }
            }
        }
        // 相同的rest，只发送一次请求
        for (let rest in typeWidgets) {
            Seq.Ajax.request({
                method: 'PUT',
                url: Ext.String.format('rest/widget/{0}', rest),
                jsonData: Ext.encode(typeWidgets[rest]),
                fnSuccess(opts, res) {
                    for (let name in res.data) {
                        let data = res.data[name];
                        if (Ext.isArray(data) && data.length == 1) {
                            res.data[name] = res.data[name][0];
                        }
                    }
                    vm.setData(res.data);
                }
            });
        }
    },

    refreshCharts(params) {
        let view = this.getView(), echarts = view.query('echartsbase');
        for (let echart of echarts) {
            let url = Ext.clone(echart.echartUrl || echart.url), params_c = Ext.clone(params);
            if (url.jsonData) {
                for (let item of url.jsonData) {
                    let defaults_c = Ext.clone(params);
                    item.tags = Seq.Array.applyIf(item.tags, defaults_c.tags);
                    delete defaults_c.tags;
                    item = Ext.applyIf(item, defaults_c);
                }
                url.jsonData = Ext.JSON.encode(url.jsonData);
            }
            delete params_c.tags;
            url.params = Ext.apply(url.params || {}, params_c);
            echart.updateUrl(url);
        }
    },

    refreshDataViewer(params) {
        let view = this.getView(), viewers = view.query('tabledataviewer');
        for (let view of viewers) {
            if (view.tableViewUrl) {
                let url = Ext.clone(view.tableViewUrl), params_c = Ext.clone(params);
                url.params = Ext.apply(url.params || {}, params_c.tags);
                view.updateUrl(url);
            }
        }
    }

})